package com.snow.useviewdemo.function.systems;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.SystemClock;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.snow.common.base.BaseBasesActivity;
import com.snow.common.tool.utils.ToastUtils;
import com.snow.useviewdemo.MainActivity;
import com.snow.useviewdemo.R;
import com.snow.useviewdemo.adapter.NAdapter;
import com.snow.useviewdemo.bean.MainListBean;
import com.snow.useviewdemo.util.HandlerHelper;
import com.snow.useviewdemo.util.phone.PhoneStartWhitelistUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * author : zyt
 * e-mail : 632105276@qq.com
 * date   : 2019-08-01
 * desc   :
 */
public class SkipToAppSetingActivity extends BaseBasesActivity {
    NAdapter nAdapter;
    private List<MainListBean> itemList;
    RecyclerView recyclerView;

    @Override
    protected int getContentViewId() {
        return R.layout.actvity_skip_to_app_seting;
    }

    @Override
    protected void initView() {
        recyclerView = findViewById(R.id.recyclerView);
        itemList = new ArrayList<>();

        recyclerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        nAdapter = new NAdapter<MainListBean>(this, R.layout.adapter_main_item, onItemClickListener) {
            @Override
            public void onBindViewHolder(NViewHolder holder, MainListBean bean, int position) {
                TextView viewById = holder.itemView.findViewById(R.id.btn_item);
                viewById.setText(bean.getName());
            }
        };
        recyclerView.setAdapter(nAdapter);
        addListData();
    }

    private void addListData() {
        itemList.add(new MainListBean("跳转设置页面", 1));
        itemList.add(new MainListBean("跳转拨打电话", 2));
        itemList.add(new MainListBean("手机信息", 3));
        itemList.add(new MainListBean("关于手机/跳转手机状态界面", 4));
        itemList.add(new MainListBean(" 跳转应用程序列表界面", 5));
//        itemList.add(new MainListBean(" 跳转快速启动设置界面", 6));//跳转崩溃，暂时不做处理
        itemList.add(new MainListBean(" 跳转手机显示界面", 7));
        itemList.add(new MainListBean(" 跳转系统的蓝牙设置界面", 8));
        itemList.add(new MainListBean("  根据包名跳转到系统自带的应用程序信息界面", 9));
        itemList.add(new MainListBean("跳转手机上应用市场--自己项目对应的页面", 10));
        itemList.add(new MainListBean("跳转Wifi列表设置 ", 11));
        itemList.add(new MainListBean("跳转设置悬浮在其他应用上层的功能 ", 12));
        itemList.add(new MainListBean("跳转QQ打开群或者申请加入群 ", 13));
        itemList.add(new MainListBean("跳转打开QQ聊天", 14));
        itemList.add(new MainListBean("跳转手机自启管理页面", 15));
        itemList.add(new MainListBean("跳转简易模式", 16));

        nAdapter.replaceData(itemList);
    }

    NAdapter.OnItemClickListener onItemClickListener = new NAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(View itemView, Object o, int position) {
            skipOption(itemList.get(position).getPosition());
        }
    };

    private void skipOption(int position) {
        Intent intent = new Intent();
        switch (position) {
            case 1:
                intent.setAction(Settings.ACTION_SETTINGS);
                startActivity(intent);
                break;
            case 2:
                intent.setAction(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:" + "17602137039"));
                if (intent.resolveActivity(getPackageManager()) != null) {
                    startActivity(intent);
                }
                break;
            case 3:
                goToSystemPage("com.android.settings.RadioInfo");
                break;
            case 4:
                intent.setAction(Settings.ACTION_DEVICE_INFO_SETTINGS);
                startActivity(intent);
                break;
            case 5:
                //跳转应用程序列表界面
//                intent.setAction(Settings.ACTION_APPLICATION_SETTINGS);
                //// 跳转到应用程序界面【所有的】
//                intent.setAction(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
                //  跳转 应用程序列表界面【已安装的】
                intent.setAction(Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS);
                startActivity(intent);
                break;
            case 6:
                intent.setAction(Settings.ACTION_QUICK_LAUNCH_SETTINGS);
                startActivity(intent);
                break;
            case 7:
                intent.setAction(Settings.ACTION_DISPLAY_SETTINGS);
                startActivity(intent);
                break;
            case 8:
                intent.setAction(Settings.ACTION_BLUETOOTH_SETTINGS);
                startActivity(intent);
                break;
            case 9:
                Uri packageURI = Uri.parse("package:" + getPackageName());
                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.setData(packageURI);
                startActivity(intent);
                break;
            case 10:
                openApplicationMarket(this);
                break;
            case 11:
                intent.setAction(Settings.ACTION_WIFI_SETTINGS);
                startActivity(intent);
                break;
            case 12:
                goToFloatPermission();
                break;
            case 13:
                String url = "mqqapi://card/show_pslcard?src_type=internal&version=1&uin=419834979&card_type=group&source=qrcode";
                Intent intent1 = new Intent(Intent.ACTION_VIEW,
                        Uri.parse(url));
                startActivity(intent1);
                break;
            case 14:
                //跳转报错：ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=mqqwpa://im/chat?chat_type=wpa&uin=632105263&version=1 }
//                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("mqqwpa://im/chat?chat_type=wpa&uin="+632105263+"&version=1")));
                String url2 = "mqqwpa://im/chat?chat_type=wpa&uin=632105263&version=1&src_type=web&web_src=oicqzone.com";
                Intent intent2 = new Intent(Intent.ACTION_VIEW,
                        Uri.parse(url2));
                intent2.addCategory("android.intent.category.DEFAULT");
                startActivity(intent2);
                break;
            case 15:
                PhoneStartWhitelistUtils.getInstance().goToPhoneStartWhitelisActivity(this);
                break;
            case 16://都跳转失败，暂时没找到可以跳转的

                break;
        }
    }

    /**
     * 跳转设置悬浮在其他应用上层的功能
     * 需要添加权限： <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
     */
    private void goToFloatPermission() {
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
        intent.setData(Uri.parse("package:" + getPackageName()));
        startActivityForResult(intent, 1);
    }
    //跳转系统设置的一些其他页面

    /**
     * 其他设置界面变量
     * 常量字段	示意
     * ACTION_SETTINGS	系统设置界面
     * ACTION_APN_SETTINGS	APN设置界面
     * ACTION_LOCATION_SOURCE_SETTINGS	定位设置界面
     * ACTION_AIRPLANE_MODE_SETTINGS	更多连接方式设置界面
     * ACTION_DATA_ROAMING_SETTINGS	双卡和移动网络设置界面
     * ACTION_ACCESSIBILITY_SETTINGS	无障碍设置界面
     * ACTION_SYNC_SETTINGS	同步设置界面
     * ACTION_ADD_ACCOUNT	添加账户界面
     * ACTION_NETWORK_OPERATOR_SETTINGS	选取运营商的界面
     * ACTION_SECURITY_SETTINGS	安全设置界面
     * ACTION_PRIVACY_SETTINGS	备份重置设置界面
     * ACTION_VPN_SETTINGS	VPN设置界面,可能不存在
     * ACTION_WIFI_SETTINGS	无线网设置界面
     * ACTION_WIFI_IP_SETTINGS	WIFI的IP设置
     * ACTION_BLUETOOTH_SETTINGS	蓝牙设置
     * ACTION_CAST_SETTINGS	投射设置
     * ACTION_DATE_SETTINGS	日期时间设置
     * ACTION_SOUND_SETTINGS	声音设置
     * ACTION_DISPLAY_SETTINGS	显示设置
     * ACTION_LOCALE_SETTINGS	语言设置
     * ACTION_VOICE_INPUT_SETTINGS	辅助应用和语音输入设置
     * ACTION_INPUT_METHOD_SETTINGS	语言和输入法设置
     * ACTION_USER_DICTIONARY_SETTINGS	个人字典设置界面
     * ACTION_INTERNAL_STORAGE_SETTINGS	存储空间设置的界面
     * ACTION_SEARCH_SETTINGS	搜索设置界面
     * ACTION_APPLICATION_DEVELOPMENT_SETTINGS	开发者选项设置
     * ACTION_DEVICE_INFO_SETTINGS	手机状态信息的界面
     * ACTION_DREAM_SETTINGS	互动屏保设置的界面
     * ACTION_NOTIFICATION_LISTENER_SETTINGS	通知使用权设置的界面
     * ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS	勿扰权限设置的界面
     * ACTION_CAPTIONING_SETTINGS	字幕设置的界面
     * ACTION_PRINT_SETTINGS	打印设置界面
     * ACTION_BATTERY_SAVER_SETTINGS	节电助手界面
     * ACTION_HOME_SETTINGS	主屏幕设置界面
     */
    private void goToSystemPage(String ss) {
        Intent intent = new Intent("/");
//        第一个参为包名，第二个各个设置的类名(可以参考下面，包名不用改变)
        ComponentName cm = new ComponentName("com.android.settings", ss);
        intent.setComponent(cm);
        intent.setAction("android.intent.action.VIEW");
        startActivity(intent);
    }

    /**
     * 通过包名 在应用商店打开应用
     *
     * @param mContext
     */
    public void openApplicationMarket(Context mContext) {
        try {
            String brand = Build.BRAND;
            String str = "market://details?id=";
            //TODO  这里根据需求需要特殊设置
            if (!TextUtils.isEmpty(brand)) {//不是国产手机没有安装国产应用商店的，只能调用链接地址
                if ("samsung".equals(brand.toLowerCase())) {//三星
                    str = "http://www.samsungapps.com/appquery/appDetail.as?appId=";
                } else if ("sony".equals(brand.toLowerCase())) {//索尼
                    str = "http://m.sonyselect.cn/";
                }
            } else {
                str = "market://details?id=";
            }
            Intent localIntent = new Intent(Intent.ACTION_VIEW);
            localIntent.setData(Uri.parse(str + "com.changdao.pupil"));
            localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mContext.startActivity(localIntent);
        } catch (Exception e) {
            // 打开应用商店失败 可能是没有手机没有安装应用市场
            e.printStackTrace();
            Toast.makeText(mContext, "打开应用商店失败", Toast.LENGTH_LONG).show();
            // 调用系统浏览器进入商城
            String url = "http://app.mi.com/detail/163525?ref=search";
            openLinkBySystem(mContext, url);
        }
    }

    /**
     * 调用系统浏览器打开网页
     *
     * @param url 地址
     */
    private void openLinkBySystem(Context mContext, String url) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(url));
        mContext.startActivity(intent);
    }

}
